import 'dart:convert';

import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zchope_student/utils/mydio.dart';
import 'package:zchope_student/utils/utils.dart';
import 'package:zchope_student/view/textfield.dart';

import '../const.dart';
import '../routes.dart';

class Login extends StatefulWidget {
  final String pageIndex;

  Login({String pageIndex}) : this.pageIndex = pageIndex;

  @override
  _LoginState createState() => _LoginState(pageIndex: pageIndex);
}

class _LoginState extends State<Login> {
  String pageIndex;
  Map data = Map<String, String>();
  var _scaffoldkey = new GlobalKey<ScaffoldState>();
  SharedPreferences sp;

  _LoginState({String pageIndex}) : this.pageIndex = pageIndex;

  @override
  void initState() {
  }

  @override
  void dispose() {
    super.dispose();
    MyDio.getInstance().clearInstance();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffededed),
      key: _scaffoldkey,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.fromLTRB(40,57,40,0),
            child: Column(
              children: <Widget>[
                Image.asset('images/login_logo.png',fit:BoxFit.fill,scale: 3,),
                SizedBox(height: 49,),
                EditText(
                  keyboardType:TextInputType.number,
                  data: data,
                  name: 'loginName',
                  labelText: '请输入手机号',
                  value: '',
                ),
                SizedBox(height: 14,),
                PassField(
                  labelText: '密码',
                  map: data,
                  mapKey: 'passwd',
                ),
                SizedBox(height: 32,),
                MaterialButton(
                  height: 44 ,
                  minWidth: MediaQuery.of(context).size.width,
                  elevation: 0,
                  onPressed: () {
                    if (data['loginName'] == null) {
                      Utils.getInstance().showSnackBar(_scaffoldkey, '用户名不能为空');
                      return;
                    }
                    if (data['passwd'] == null) {
                      Utils.getInstance().showSnackBar(_scaffoldkey, '密码不能为空');
                      return;
                    }
                    Routes.router.navigateTo(context, '${Routes.mainpage}',
                        replace: true, transition: TransitionType.fadeIn);
                    // 登录
//              MyDio.getInstance().post(Apis.login, (data) async {
//                Utils.getInstance().showSnackBar(_scaffoldkey, data['msg']);
//
//                if (data['resultCode'] == 0) {
//                  Utils.getInstance().showSnackBar(_scaffoldkey, data['msg']);
//                  LoginBean loginBean = LoginBean.fromJson(data['record']);
//                  MyDio.getInstance().get(Apis.memberInfo + loginBean.recordBh, (dataUser) async {
//                    print('memberInfo=====${data}');
//                    if (dataUser['resultCode'] == 0) {
//                      UserInfoBean _userInfoBean = UserInfoBean.fromJson(dataUser['record']);
//                      sp = await SharedPreferences.getInstance();
//                      sp.clear();
//                      sp.setString(Const.PERSON_NUM, _userInfoBean.personNum);
//                      sp.setString(Const.UUID, _userInfoBean.recordBh);
//                      sp.setString('loginName', _userInfoBean.loginName);
//                      sp.setString('nickName', _userInfoBean.nickName);
//                      sp.setString('photo', _userInfoBean.photo);
//                      sp.setString(Const.AREA_CODE, _userInfoBean.areacode);
//                      eventBus.fire(EventBean(title:Const.EVENT_LOGIN));
//                      Navigator.pop(context);
//                    }else{
//                      Utils.getInstance().showSnackBar(_scaffoldkey, data['msg']);
//                    }
//                  });
//                }
//              }, params: data);
                  },
                  child: Text(
                    "登录",
                    style: TextStyle(color: Colors.white, fontSize: 18),
                  ),
                  color: Const.blue,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(
                          Radius.circular(88/3*2))),
                ),
              ],
            ),
          ),
          Image.asset('images/login_bg.png')
        ],
      ),
    );
  }

  String _md5Digest(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    return hex.encode(digest.bytes);
  }
}